﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class winkelwagen : System.Web.UI.Page
{
    List<Game> games;
    List<Game> covers;
        
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            BindGridView();
        }
        else
        {
            winkel_content.Visible = false;
            nietingelogd.Text = "U moet ingelogd zijn om de winkelwagen te bekijken";
            totaal.Text = "0.00";
        }
    }
    private void BindGridView()
    {
        Decimal prijs = new decimal();
        Decimal totaalbestand = new decimal();

        games = (List<Game>)Session["winkelwagen"];
        if (games != null)
        {
            foreach (Game game in games)
            {
                prijs += game.PRIJS;
                totaalbestand += game.BESTANDSGROOTTEGAME;
            }
            GridView1.DataSource = games;
            GridView1.DataBind();
        }

        covers = (List<Game>)Session["winkelwagenCover"];
        if (covers != null)
        {
            foreach (Game cover in covers)
            {
                prijs += (Decimal)0.5;
                totaalbestand += cover.BESTANDSGROOTTEAFBEELDING;
            }

            GridView2.DataSource = covers;
            GridView2.DataBind();
        }
        else
        {
            hoesjeskosten.Text = "U heeft geen hoesjes in de winkelwagen geplaatst";
        }
        
        if (games != null || covers != null)
        {
            totaal.Text = prijs.ToString();
            bestandsgrootte.Text = totaalbestand.ToString() + "MB";
            if (totaalbestand > 10000)
            {
                bestandsgrootte.ForeColor = Color.Orange;
                bestandsgrootte.ToolTip = "De grootte van uw bestanden bedraagt meer dan 10000MB, Dit kan voor een lange download tijd zorgen. ";
            }
            else
                bestandsgrootte.ForeColor = Color.Green;
        }
        else
        {
            nietingelogd.Text = "U heeft geen producten in uw winkelwagen";
            winkel_content.Visible = false;
        }
    }

    protected void GridView1_RowDeleted(object sender, GridViewDeleteEventArgs e)
    {
        GridView1.DeleteRow(e.RowIndex);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       string id = GridView1.Rows[e.RowIndex].Cells[1].Text;// get the id of the selected row
       DeleteRecord(id, e.RowIndex, 1);//call delete method
       BindGridView();//rebind grid to reflect changes made
    }

    protected void GridView2_RowDeleted(object sender, GridViewDeleteEventArgs e)
    {
        GridView2.DeleteRow(e.RowIndex);
    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView2.Rows[e.RowIndex].Cells[1].Text;// get the id of the selected row
        DeleteRecord(id, e.RowIndex, 2);//call delete method
        BindGridView();//rebind grid to reflect changes made
    }

    private void DeleteRecord(string ID, int index, int grid)
    {
        if (grid == 1)
            games.RemoveAt(index);
        else
            covers.RemoveAt(index);
        this.Master.refreshControl();
    }
}
